'''
case Type :数据库系统
case Name ：主机创建表空间，备机存在相应目录但无权限，备机stop，恢复权限重启备机，表空间创建成功
Create At: 2020-07-1
owner : @peilinqian
description:1、查看备机是否存在相应目录，无目录创建目录2、修改目录权限为000 3、主机进行建表空间4、查看备机stop及相应日志5、修改备机目录权限为700，重启备机6、查询备机表空间
history：
created： 2020/7/30  created by @peilinqian
'''

import os
import unittest
import time
import _thread
import glob
from yat.test import Node
from testcase.utils.CommonSH import *
from yat.test import macro

from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import *

from testcase.utils.Common import *
from testcase.utils.PgSql import PgSql


class MyTest(unittest.TestCase):
    primary_sh = CommonSH('PrimaryDbUser')
    standby1_sh = CommonSH('Standby1DbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('------------Opengauss_Reliability_Dbsys_Case014.py start------------')
        self.rootNode = Node(node='PrimaryRoot')
        self.dbUserNode = Node(node='PrimaryDbUser')
        self.standbyrootNode = Node(node='Standby1Root')
        self.standbydbUserNode = Node(node='Standby1DbUser')
        self.DB_INSTANCE_PATH = macro.DB_INSTANCE_PATH
        self.DN_NODE_NAME = macro.DN_NODE_NAME
        self.PG_LOG_PATH = macro.PG_LOG_PATH
        self.space_path = os.path.join(os.path.dirname(self.DB_INSTANCE_PATH), 'test_space')
        self.standbyrootNode2 = Node(node='Standby2Root')
        self.com = Common()
        self.Constant = Constant()

    def test_main(self):
        self.log.info("查看备机是否存在相应目录，无目录进行创建")
        cmd = f'ls {self.space_path}'
        self.log.info(cmd)
        msg = self.standbydbUserNode.sh(cmd).result()
        if self.Constant.NO_FILE_MSG in msg:
            cmd = f'mkdir {self.space_path}'
            self.log.info(cmd)
            msg = self.standbydbUserNode.sh(cmd).result()
            self.assertEqual(msg, '')
            self.log.info('mkdir path success')

        # 修改备机目录权限000
        chmod_cmd = 'chmod 000 {path}'.format(path=self.space_path)
        self.log.info(chmod_cmd)
        chmodmsg = self.standbyrootNode.sh(chmod_cmd).result()
        self.log.info('*****chmod 000 path*****' + chmodmsg)

        # 执行创建表空间
        create_space_cmd = "create tablespace space_test owner {dbuser} location \'{path}\';".format(
            dbuser=self.dbUserNode.ssh_user, path=self.space_path)
        self.log.info(create_space_cmd)
        self.assertIn('CREATE TABLESPACE', self.primary_sh.executDbSql(create_space_cmd))

        # 查看备机状态stopped
        db_status = self.primary_sh.getDbClusterStatus('status')
        self.assertFalse(db_status)

        # 查看日志关键信息
        findfile_cmd = 'ls -t ' + self.PG_LOG_PATH + '/' + self.DN_NODE_NAME.split('/')[1] + '|head -n1'
        self.log.info(findfile_cmd)
        msg = self.standbydbUserNode.sh(findfile_cmd).result()
        self.log.info(msg)

        catfile_cmd = 'cat ' + self.PG_LOG_PATH + '/' + self.DN_NODE_NAME.split('/')[1] + '/' + msg
        self.log.info(catfile_cmd)
        msg = self.standbydbUserNode.sh(catfile_cmd).result()
        failed_msg = 'FATAL:  could not access directory "{path}": Permission denied'.format(path=self.space_path)
        self.log.info(msg)
        self.assertIn(failed_msg, msg)

        # 修改备机目录权限700
        chmod_cmd = 'chmod 700 {path}'.format(path=self.space_path)
        chmodmsg = self.standbyrootNode.sh(chmod_cmd).result()
        self.log.info('*****chmod 700 path*****' + chmodmsg)

        # 重启备机
        msg = self.primary_sh.startDbCluster()
        self.assertTrue(msg)

        time.sleep(3)

        # 执行查询备机存在相应表空间目录
        cat_space_cmd = r'\db ;'
        self.assertIn(self.space_path, self.primary_sh.executDbSql(cat_space_cmd))
        self.log.info('standbydb exist space')

    def tearDown(self):
        # 删除表空间
        drop_space_cmd = 'DROP TABLESPACE space_test ;'
        result = self.primary_sh.executDbSql(drop_space_cmd)
        self.log.info(result)

        chmod_cmd = 'chmod 700 {path}'.format(path=self.space_path)
        chmodmsg = self.standbyrootNode.sh(chmod_cmd).result()
        self.log.info('*****chmod 700 path*****' + chmodmsg)
        # 删除目录
        self.log.info("---------------------------删除备1表空间路径----------------------")
        cmd = f'rm -rf {self.space_path}'
        self.log.info(cmd)
        msg = self.standbyrootNode.sh(cmd).result()
        self.log.info(msg)
        self.log.info("---------------------------删除主表空间路径----------------------")
        msg = self.rootNode.sh(cmd).result()
        self.log.info(msg)
        self.log.info("---------------------------删除备2表空间路径----------------------")
        msg = self.standbyrootNode2.sh(cmd).result()
        self.log.info(msg)
